package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyNoticeDao;
import ex.com.jm.food.entity.Notices;

public class CompanyNoticeDaoImpl extends HibernateDaoSupport implements
		CompanyNoticeDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Notices> getNotices(final int initPage,final int pageRow,final String comId,
			final String title,final String s_date,final String e_date,final String order,
			final String sort) {
		// TODO Auto-generated method stub
		Object notices=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Notices where comId='"+comId+"' ";
				if (title!=null&&!title.equals("")) {
					hql+=" and noticeTitle like '%"+title+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and created<='"+e_date+"'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else {
					hql+=" order by created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Notices>) notices;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int noticeCount(final String comId,final String title,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Notices where comId='"+comId+"' ";
				if (title!=null&&!title.equals("")) {
					hql+=" and noticeTitle like '%"+title+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and created<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveNotice(Notices notices) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(notices);
	}

	@Override
	public void updateNotices(Notices notices) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().update(notices);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" update Notices set status="+status+" where noticeId='"+id+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if (count==1) {
			return true;
		}
		return false;
	}

	@Override
	public Notices editNotices(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Notices.class, id);
	}

	@Override
	public void delNotice(String id) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Notices.class, id));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Notices checkNotices(final String comId) {
		// TODO Auto-generated method stub
		Notices notices=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Notices where comId='"+comId+"' and status=1";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return notices;
	}

}
